package com.dcy.workflow.controller;

import com.dcy.common.base.controller.DcyBaseController;
import com.dcy.common.model.PageModel;
import com.dcy.common.model.PageResult;
import com.dcy.common.model.R;
import com.dcy.common.utils.LoginUtil;
import com.dcy.workflow.service.ActTaskService;
import com.dcy.workflow.vo.in.TaskHisSearchInVO;
import com.dcy.workflow.vo.in.TaskRunSearchInVO;
import com.dcy.workflow.vo.out.ActivityInstanceListOutVO;
import com.dcy.workflow.vo.out.HisTaskListOutVO;
import com.dcy.workflow.vo.out.TaskListOutVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @Author：dcy
 * @Description: 任务操作接口
 * @Date: 2021/6/11 15:36
 */
@RequiredArgsConstructor
@Validated
@Slf4j
@RestController
@RequestMapping("/flow/task")
@Api(value = "TaskController", tags = {"任务操作接口"})
public class TaskController extends DcyBaseController {

    private final ActTaskService actTaskService;

    //========================任务节点信息===================================

    @ApiOperation(value = "获取运行任务节点信息")
    @ApiImplicitParam(name = "processInstanceId", value = "流程实例id", dataType = "String", paramType = "query")
    @GetMapping(value = "/getRunTaskInsList")
    public R<List<ActivityInstanceListOutVO>> getRunTaskInsList(@NotBlank(message = "流程实例id不能为空") String processInstanceId) {
        return success(actTaskService.getRunTaskInsList(processInstanceId));
    }

    @ApiOperation(value = "根据任务id获取历史任务节点信息")
    @ApiImplicitParam(name = "taskId", value = "任务id", dataType = "String", paramType = "query")
    @GetMapping(value = "/getHisTaskInsListByTaskId")
    public R<List<ActivityInstanceListOutVO>> getHisTaskInsListByTaskId(@NotBlank(message = "任务id不能为空") String taskId) {
        return success(actTaskService.getHisTaskInsListByTaskId(taskId));
    }

    @ApiOperation(value = "根据流程实例id获取历史任务节点信息")
    @ApiImplicitParam(name = "processInstanceId", value = "流程实例id", dataType = "String", paramType = "query")
    @GetMapping(value = "/getHisTaskInsListByProInsId")
    public R<List<ActivityInstanceListOutVO>> getHisTaskInsListByProInsId(@NotBlank(message = "流程实例id不能为空") String processInstanceId) {
        return success(actTaskService.getHisTaskInsListByProInsId(processInstanceId));
    }

    @ApiOperation(value = "获取任务节点信息（先查询运行的，如果没有在查历史的）")
    @ApiImplicitParam(name = "processInstanceId", value = "流程实例id", dataType = "String", paramType = "query")
    @GetMapping(value = "/getTaskInsList")
    public R<List<ActivityInstanceListOutVO>> getTaskInsList(@NotBlank(message = "流程实例id不能为空") String processInstanceId) {
        return success(actTaskService.getTaskInsList(processInstanceId));
    }


    //========================任务信息===================================

    @ApiOperation(value = "获取代办任务")
    @GetMapping(value = "/getRunTaskList")
    public R<PageResult<TaskListOutVO>> getRunTaskList(TaskRunSearchInVO taskRunSearchInVO, PageModel pageModel) {
        return success(actTaskService.getRunTaskList(taskRunSearchInVO, pageModel, LoginUtil.getUserInfo().getId()));
    }

    @ApiOperation(value = "获取已办任务")
    @GetMapping(value = "/getHisTaskList")
    public R<PageResult<HisTaskListOutVO>> getHisTaskList(TaskHisSearchInVO taskHisSearchInVO, PageModel pageModel) {
        return success(actTaskService.getHisTaskList(taskHisSearchInVO, pageModel, LoginUtil.getUserInfo().getId()));
    }

    //========================完成任务===================================

    @ApiOperation(value = "完成任务", notes = "适用于单纯的审批，不需要批注信息的")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "taskId", value = "任务ID", dataType = "String", paramType = "query", required = true)
    })
    @PostMapping(value = "/completeTask")
    public R<Boolean> completeTask(@NotBlank(message = "任务id不能为空") String taskId) {
        actTaskService.complete(taskId, LoginUtil.getUserInfo().getId());
        return success(true);
    }

    @ApiOperation(value = "完成任务并且添加批注信息+通过状态", notes = "适用于单纯的审批，没有任何的分支")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "taskId", value = "任务ID", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "comment", value = "审批意义", dataType = "String", paramType = "query")
    })
    @PostMapping(value = "/completeTaskAndComment")
    public R<Boolean> completeTaskAndComment(@NotBlank(message = "任务id不能为空") String taskId,
                                             @NotBlank(message = "审批意见不能为空") String comment) {
        actTaskService.complete(taskId, LoginUtil.getUserInfo().getId(), comment);
        return success(true);
    }

    @ApiOperation(value = "完成任务并且添加批注信息+通过状态+批注状态", notes = "适用于单纯的审批，没有任何的分支，有审批的状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "taskId", value = "任务ID", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "comment", value = "审批意义", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "adopt", value = "是否通过", dataType = "Boolean", paramType = "query")
    })
    @PostMapping(value = "/completeTaskAndCommentAndAdopt")
    public R<Boolean> completeTaskAndCommentAndAdopt(@NotBlank(message = "任务id不能为空") String taskId,
                                                     @NotBlank(message = "审批意见不能为空") String comment,
                                                     @NotNull(message = "是否通过不能为空") Boolean adopt) {
        actTaskService.complete(taskId, LoginUtil.getUserInfo().getId(), comment, adopt);
        return success(true);
    }

    @ApiOperation(value = "完成任务并且添加批注信息+通过状态+批注状态+流程变量", notes = "适用于单纯的审批，没有任何的分支，有审批的状态，有流程变量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "taskId", value = "任务ID", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "comment", value = "审批意义", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "adopt", value = "是否通过", dataType = "Boolean", paramType = "query")
    })
    @PostMapping(value = "/completeTaskAndCommentAndSetVar")
    public R<Boolean> completeTaskAndCommentAndSetVar(@NotBlank(message = "任务id不能为空") String taskId,
                                                      @NotBlank(message = "审批意见不能为空") String comment,
                                                      @NotNull(message = "是否通过不能为空") Boolean adopt) {
        actTaskService.completeAndSetVar(taskId, LoginUtil.getUserInfo().getId(), comment, adopt);
        return success(true);
    }

}
